/**
 * =========================================================
 * Creation Time : 2015年8月29日
 * =========================================================
 * Copyright@ 2015 SHENZHEN CLOU ELECTRONICS CO., LTD.
 * All right reserved. 
 */
package com.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;



/**
 * Functional Description:
 *
 *@author：BaiJun
 */
public class DeployPropertiesUtil {
   
	/**
	 * 加载配置文件
	 * 
	 * @param url
	 * @return
	 */
	public ResourceBundle loadProperties(){
		ResourceBundle bundle = null;
		
		//检测是否存在配置文件
		try{
			
//			System.getProperty("java.class.path")
//			System.getProperty("user.dir")
			String filePath = System.getProperty("user.dir");
			String filePathSub=filePath.substring(0, filePath.length())+"\\conf\\system-config.properties";
			//		+ "/conf/init.properties";

			bundle = new PropertyResourceBundle(new BufferedInputStream(new FileInputStream(filePathSub)));
			
		}catch(Exception ex){
			System.out.println("loadProperties: "+ ex);
		}

		
		return bundle;
	}
	
	/**
	 * 获取属性值
	 * 
	 * @param name
	 * @return
	 */
	public String readPropertyByNameFromProperties(String name,String url){
		ResourceBundle boundle = loadProperties();
		String property = null;
		if(boundle != null){
			try {
				property = boundle.getString(name);
			} catch (Exception e) {
				System.out.println("getPropertyByNameFromProperties:　"+ e);
			}
		}
		return property;
	}
	
	/**
	 * 读数据
	 * 
	 * @param name
	 * @return
	 */
	public String readPropertyByNameFromProperties(String name){
		ResourceBundle boundle = loadProperties();
		String property = null;
		if(boundle != null){
			try {
				property = boundle.getString(name);
			} catch (Exception e) {
				System.out.println("getPropertyByNameFromProperties:　"+ e);
			}
		}
		return property;
	}
	
	/**
	 * 修改后的读操作
	 * @param name
	 * @return
	 */
//	public String readPropertyByNameFromProperties(String name){
//		Properties properties = new Properties();
//		String path = System.getProperty("java.class.path"); //打成jar包后
////		String path = System.getProperty("user.dir"); //项目路径
//		int index = path.lastIndexOf(File.separator);
//		path = path.substring(0, index +1) + "init.properties";
//		String value = null;
//		try {
//			InputStream stream = new FileInputStream(path);
//			properties.load(stream);
//			stream.close();//关闭流
//			value = properties.getProperty(name);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return value;
//	} 
	/**
	 * 写入数据
	 * @param name
	 * @param value
	 * @param path
	 * @return
	 */
	public boolean writeProperty2Properties(String name,String value,String path){
		OutputStream os = null;
		boolean bool = false;
		Properties props = new Properties();
		try {
	    	os = new FileOutputStream(path,true);
	    	props.setProperty(name, value);
			props.store(os, "write");
			os.flush();
			bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * 写数据
	 * 
	 * @param name
	 * @param value
	 * @param path
	 * @return
	 */
//	public boolean writeValue2Properties(String name,String value,String path){
//		ResourceBundle boundle = loadProperties();
//	}
	
	public static void main(String[] args) {
//		String path = System.getProperty("user.dir");
//		int index = System.getProperty("user.dir").lastIndexOf(File.separator);
//		path = path.substring(0, index +1);
//		String url = path +"\\src\\main\\java\\com\\clou\\power7000\\fep\\common\\allocation\\init.properties";
////		System.out.println("path: "+ path);
//		new DeployPropertiesUtil().writeProperty2Properties("Fep_IP", "10.98.103.6", url);
//		new DeployPropertiesUtil().writeProperty2Properties("Fep_Port", "1000", url);
//		new DeployPropertiesUtil().writeProperty2Properties("Redis_IP", "10.98.103.6", url);
//		new DeployPropertiesUtil().writeProperty2Properties("Redis_Port", "6379", url);
		String Redis_Port = new DeployPropertiesUtil().readPropertyByNameFromProperties("Redis_Port");
		System.out.println("path: "+Redis_Port);
	}
	
}
